package com.fudan.backend.controller;

import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.CommodityLaunchRequest;
import com.fudan.backend.service.CommodityLaunchRequestService;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/commodity/launch-request")
public class CommodityLaunchRequestController {
    @Autowired
    CommodityLaunchRequestService commodityLaunchRequestService;

    @PostMapping()
    public R launchRequest(@RequestBody CommodityLaunchRequest request){
        commodityLaunchRequestService.addCommodityLaunchRequest(request);
        return new R(RetCode.COMMODITY_SUCCESS);
    }
    @PostMapping("/process")
    public R processLaunchRequest(@RequestBody CommodityLaunchRequest request){
        commodityLaunchRequestService.process(request);
        return new R(RetCode.COMMODITY_SUCCESS);
    }
    @GetMapping()
    public R getAllLaunchRequest(Integer current, Integer size){
        PageInfo<CommodityLaunchRequest> page;
        page = commodityLaunchRequestService.getAllCommodityLaunchRequestByPage(current, size);

        Map<String, Object> ret = new HashMap<>();
        ret.put("launchRequestList", page.getList());
        ret.put("total", page.getTotal());
        return new R(RetCode.COMMODITY_SUCCESS, ret);
    }

    @GetMapping("/store/{storeId}")
    public R getStoreLaunchRequest(Integer current, Integer size, @PathVariable("storeId") String storeId){
        PageInfo<CommodityLaunchRequest> page;
        page = commodityLaunchRequestService.getStoreCommodityLaunchRequestByPage(storeId, current, size);
        Map<String, Object> ret = new HashMap<>();
        ret.put("launchRequestList", page.getList());
        ret.put("total", page.getTotal());
        return new R(RetCode.COMMODITY_SUCCESS, ret);
    }
}
